import { defineStore } from "pinia"
import {ref} from 'vue'
import {loginAPI} from '@/apis/user'
import {useCartStore } from './cartStore'
import { mergeCart } from "@/apis/cart"
export const useUserStore=defineStore('user',()=>{
    //定义用户管理数据的state
    const userInfo=ref({})
    const cartStore=useCartStore()
    //定义获取接口数据的action函数
    const getUserInfo=async({account,password})=>{
        const res=await loginAPI({account,password})
        userInfo.value=res.result
        //合并购物车
       await mergeCart(cartStore.cartList.map(item=>{
            return{
                skuId:item.skuId,
                selected:item.selected,
                count:item.count
            }
        }))
        cartStore.updateNewList()
    }
    const clearUserinfo=()=>{
        userInfo.value={}
        cartStore.clearCart()
    }
    //已对象的形式导出
    return{
        userInfo,
        getUserInfo,
        clearUserinfo
    }
},{
    persist:true
})